<template>
  <view class="page_diy_view page_payment_information_view" :style="{ paddingTop: vuex_custom_bar_height + 'px' }">
    <tn-nav-bar>缴费信息详情</tn-nav-bar>
    <view id="page_diy_view" class="page-from">
      <uni-forms :modelValue="form" v-if="is_view()" labelWidth="70px">

        <uni-forms-item v-if="$check_field('get','student_name') || ($check_field('add','student_name') || $check_field('set','student_name'))" label="学生姓名" name="student_name">
                      <uni-easyinput type="text" v-model="form['student_name']" v-if="(form['payment_information_id'] && $check_field('set','student_name')) || (!form['payment_information_id'] && $check_field('add','student_name'))" :disabled="disabledObj['student_name_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_name')">
            {{ form['student_name'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_account') || ($check_field('add','student_account') || $check_field('set','student_account'))" label="学生账户" name="student_account">
              <uni-data-select
              id="form_student_account"
              v-model="form['student_account']"
              :localdata="list_user_student_account"
              :clear="!disabledObj['student_account_isDisabled']"
              :disabled="disabledObj['student_account_isDisabled']"
              v-if="(form['payment_information_id'] && $check_field('set', 'student_account')) || (!form['payment_information_id'] && $check_field('add', 'student_account'))"
			   style="width: 100%;"
          ></uni-data-select>
          <uni-data-select
              v-model="form['student_account']"
              :localdata="list_user_student_account"
              :clear="false"
              :disabled="true"
              v-else-if="$check_field('get', 'student_account')"
              id="student_account"
			  style="width: 100%;"
          ></uni-data-select>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_id') || ($check_field('add','student_id') || $check_field('set','student_id'))" label="学生学号" name="student_id">
                      <uni-easyinput type="text" v-model="form['student_id']" v-if="(form['payment_information_id'] && $check_field('set','student_id')) || (!form['payment_information_id'] && $check_field('add','student_id'))" :disabled="disabledObj['student_id_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_id')">
            {{ form['student_id'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_director') || ($check_field('add','class_director') || $check_field('set','class_director'))" label="班级主任" name="class_director">
              <uni-data-select
              id="form_class_director"
              v-model="form['class_director']"
              :localdata="list_user_class_director"
              :clear="!disabledObj['class_director_isDisabled']"
              :disabled="disabledObj['class_director_isDisabled']"
              v-if="(form['payment_information_id'] && $check_field('set', 'class_director')) || (!form['payment_information_id'] && $check_field('add', 'class_director'))"
			   style="width: 100%;"
          ></uni-data-select>
          <uni-data-select
              v-model="form['class_director']"
              :localdata="list_user_class_director"
              :clear="false"
              :disabled="true"
              v-else-if="$check_field('get', 'class_director')"
              id="class_director"
			  style="width: 100%;"
          ></uni-data-select>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','payment_type') || ($check_field('add','payment_type') || $check_field('set','payment_type'))" label="缴费类型" name="payment_type">
                      <uni-easyinput type="text" v-model="form['payment_type']" v-if="(form['payment_information_id'] && $check_field('set','payment_type')) || (!form['payment_information_id'] && $check_field('add','payment_type'))" :disabled="disabledObj['payment_type_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','payment_type')">
            {{ form['payment_type'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','payment_amount') || ($check_field('add','payment_amount') || $check_field('set','payment_amount'))" label="缴费金额" name="payment_amount">
                      <uni-easyinput type="text" v-model="form['payment_amount']" v-if="(form['payment_information_id'] && $check_field('set','payment_amount')) || (!form['payment_information_id'] && $check_field('add','payment_amount'))" :disabled="disabledObj['payment_amount_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','payment_amount')">
            {{ form['payment_amount'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','cost_details') || ($check_field('add','cost_details') || $check_field('set','cost_details'))" label="费用详情" name="cost_details">
              <uni-easyinput type="textarea" v-model="form['cost_details']" v-if="(form['payment_information_id'] && $check_field('set', 'cost_details')) || (!form['payment_information_id'] && $check_field('add', 'cost_details'))" :disabled="disabledObj['cost_details_isDisabled']"/>
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get', 'cost_details')">
            {{ form['cost_details'] }}
          </text>
            </uni-forms-item>
      </uni-forms>
      <view class="form-footer" v-if="$check_action('/payment_information/view','set') || ($check_action('/payment_information/view','add') || $check_option('/payment_information/table','examine'))">
        <view class="me-btn btn-submit" @click="submit_()"> 提交 </view>
        <view class="me-btn btn-cancel" @click="cancel()">取消</view>
      </view>
      <view class="form_button" v-else>
        <view class="me-btn btn-cancel" @click="cancel()">返回</view>
      </view>
    </view>
  </view>
</template>

<script>
import mixin from '@/libs/mixins/page.js';
                            import { uploadFilePathApi, uploadFileApi } from '@/api/common.js';
export default {
  mixins: [mixin],
  data() {
    return {
	  today: new Date().toISOString().split('T')[0],
      field: "payment_information_id",
      url_add: "~/api/payment_information/add?",
      url_set: "~/api/payment_information/set?",
      url_get_obj: "~/api/payment_information/get_obj?",
      url_upload: "~/api/payment_information/upload?",
      student_name: null,
      student_account: null,
      student_id: null,
      class_director: null,
      payment_type: null,
      payment_amount: null,
      cost_details: null,
      query: {
        "payment_information_id": 0,
      },
      form: {
          "student_name":  '', // 学生姓名
            "student_account": 0, // 学生账户
            "student_id":  '', // 学生学号
            "class_director": 0, // 班级主任
            "payment_type":  '', // 缴费类型
            "payment_amount":  0 , // 缴费金额
            "cost_details":  '', // 费用详情
          "payment_information_id": 0, // ID
      },
      disabledObj:{
          "student_name_isDisabled": false,
            "student_account_isDisabled": false,
            "student_id_isDisabled": false,
            "class_director_isDisabled": false,
            "payment_type_isDisabled": false,
                "cost_details_isDisabled": false,
        },
                      // 用户列表
      list_user_student_account: [],
                                  // 用户列表
      list_user_class_director: [],
                  // 用户组
      group_user_class_director: "",
                                        };
  },
  methods: {
    /**
     * 提交前验证事件
     * @param {Object} 请求参数
     * @return {String} 验证成功返回null, 失败返回错误提示
     */
    async submit_check(param) {
                                                                                                                      return null;
    },
    changeLog(v, value) {
      this.form[value] = v;
    },
    uploadFile_(type) {
      // #ifdef APP-VUE
      uni.chooseFile({
        count: 1,
        success: (chooseImageRes) => {
          const tempFilePaths = chooseImageRes.tempFilePaths;
          this.successChoose(tempFilePaths[0], type);
        },
      });
      // #endif
      // #ifdef !APP-VUE
      if (
        uni.getSystemInfoSync().uniPlatform != 'mp-weixin' ||
        uni.getSystemInfoSync().platform == 'devtools'
      ) {
        uni.chooseImage({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            this.successChoose(tempFilePaths[0], type);
          },
        });
      } else {
        wx.chooseMessageFile({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFiles;
            this.successChoose(tempFilePaths[0].path, type);
          },
        });
      }
      // #endif
    },
    successChoose(filePath, type) {
      uni.uploadFile({
        url: this.$fullUrl('~/api/payment_information/upload?'), //仅为示例，非真实的接口地址
        filePath,
        name: 'file',
        success: (uploadFileRes) => {
          if (uploadFileRes.data.error) {
            uni.showToast({ title: uploadFileRes.data.error.message, icon: 'none' });
          } else {
            this[type] = JSON.parse(uploadFileRes.data).result.url;
          }
        },
      });
    },
    close_(type) {
      if (type == 'student_name') this['student_name'] = this.form['student_name'] = "";
      if (type == 'student_account') this['student_account'] = this.form['student_account'] = "";
      if (type == 'student_id') this['student_id'] = this.form['student_id'] = "";
      if (type == 'class_director') this['class_director'] = this.form['class_director'] = "";
      if (type == 'payment_type') this['payment_type'] = this.form['payment_type'] = "";
      if (type == 'payment_amount') this['payment_amount'] = this.form['payment_amount'] = "";
      if (type == 'cost_details') this['cost_details'] = this.form['cost_details'] = "";
    },
    submit_() {
      if (this['student_name'] !== null) this.form['student_name'] = this['student_name']
      if (this['student_account'] !== null) this.form['student_account'] = this['student_account']
      if (this['student_id'] !== null) this.form['student_id'] = this['student_id']
      if (this['class_director'] !== null) this.form['class_director'] = this['class_director']
      if (this['payment_type'] !== null) this.form['payment_type'] = this['payment_type']
      if (this['payment_amount'] !== null) this.form['payment_amount'] = this['payment_amount']
      if (this['cost_details'] !== null) this.form['cost_details'] = this['cost_details']
      console.log(this.form)
      this.submit();
    },
    /**
     * 上传文件
     * @param {Object} param文件参数
     */
    change_file(key_name) {
      var _self = this;
      // 选择图像方法
      uni.chooseFile({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi(
            '/payment_information/upload',
            filePath,
            undefined,
            {
              i_want_to_customize: 'test',
            },
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 上传图片
     * @param {Object} param文件参数
     */
    change_img(key_name) {
      var _self = this;
      _self.upload_img_flag = false;
      // 选择图像方法
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi('/payment_information/upload',filePath,undefined,undefined,
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 获取对象后获取缓存表单
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_before(param) {
      var form = uni.db.get('form');
      if (form) {
        delete form.examine_state;
        delete form.examine_reply;
        this.obj = uni.push(this.obj, form);
        this.form = uni.push(this.form, form);
      }
      var arr = [];
      for (let key in form) {
        arr.push(key);
      }
      for (var i = 0; i < arr.length; i++) {
        this.disabledObj[arr[i] + '_isDisabled'] = true;
      }
                                  uni.db.del('form');

      return param;
    },
    
          /**
     * 获取学生用户用户列表
     */
    async get_list_user_student_account() {
      var json = await this.$get("~/api/user/get_list?user_group=学生用户");
      if(json.result && json.result.list){
        json.result.list.map((o) => this.list_user_student_account.push({value:o.user_id,text:o.nickname + '-' + o.username}));
      }
      else if(json.error){
        console.error(json.error);
      }
    },
      
      
          /**
     * 获取教师用户用户列表
     */
    async get_list_user_class_director() {
      var json = await this.$get("~/api/user/get_list?user_group=教师用户");
      if(json.result && json.result.list){
        json.result.list.map((o) => this.list_user_class_director.push({value:o.user_id,text:o.nickname + '-' + o.username}));
      }
      else if(json.error){
        console.error(json.error);
      }
    },
        /**
     * 获取教师用户用户组
     */
    async get_group_user_class_director() {
      this.form["class_director"] = this.user.user_id;
      var json = await this.$get("~/api/user_group/get_obj?name=教师用户");
      if(json.result && json.result.obj){
        this.group_user_class_director = json.result.obj;
        this.get_user_session_class_director(this.form['class_director'])
      }
      else if(json.error){
        console.error(json.error);
      }
    },
    get_user_session_class_director(id){
      var _this = this;
      var user_id = {"user_id":_this.user.user_id}
      var url = "~/api/"+_this.group_user_class_director.source_table+"/get_obj"
      this.$get(url, user_id, function(res) {
        if (res.result && res.result.obj) {
          var arr = []
          for (let key in res.result.obj) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in _this.form) {
            arrForm.push(key)
          }
          _this.form["class_director"] = _this.user.user_id
          _this.disabledObj['class_director' + '_isDisabled'] = true
          for (var i=0;i<arr.length;i++){
            if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arr[i] === arrForm[j]) {
                  if (arr[i] !== "class_director") {
                    _this.form[arrForm[j]] = res.result.obj[arr[i]]
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                    break;
                  } else {
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  }
                }
              }
            }
          }
        }
      });
    },
      
      
      
      
      /**
     * 获取对象之后
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func) {
                                                                                        },

    is_view() {
      var bl = this.user_group == '管理员';

      if (!bl) {
        bl = this.$check_action('/payment_information/table', 'add');
        console.log(bl ? '你有表格添加权限视作有添加权限' : '你没有表格添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/payment_information/table', 'set');
        console.log(bl ? '你有表格添加权限视作有修改权限' : '你没有表格修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/payment_information/view', 'add');
        console.log(bl ? '你有视图添加权限视作有添加权限' : '你没有视图添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/payment_information/view', 'set');
        console.log(bl ? '你有视图修改权限视作有修改权限' : '你没有视图修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/payment_information/view', 'get');
        console.log(bl ? '你有视图查询权限视作有查询权限' : '你没有视图查询权限');
      }

      console.log(
        bl
          ? '具有当前页面的查看权，请注意这不代表你有字段的查看权'
          : '无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行'
      );

      return bl;
    },
  },
  created() {
            this.get_list_user_student_account();
                    this.get_list_user_class_director();
          this.get_group_user_class_director();
                      },
};
</script>

<style lang="scss" scoped>
  @import '@/styles/pagesC/index.scss';
</style>